<template>
  <div id="login-box" v-loading="loading">
    <div class="box-card">
      <!--头像区域-->
      <div class="box-portrait">
        <img src="../assets/images/Chole.jpg">
      </div>
      <el-form ref="loginRef" :rules="loginRules" :model="loginForm" label-width="0">
        <!--用户名区域-->
        <el-form-item prop="name">
          <el-input prefix-icon="el-icon-user" placeholder="请输入用户名" v-model="loginForm.name"></el-input>
        </el-form-item>
        <!--密码区域-->
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" placeholder="请输入密码" type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <!--按钮区域-->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 表单数据
      loginForm: {
        name: 'chole',
        password: '123456'
      },
      loading: false,
      // 表单验证对象
      loginRules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 12, message: '长度在 5 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击重置按钮重置表单
    resetLoginForm () {
      this.$refs.loginRef.resetFields()
    },
    // 登录方法
    login () {
      this.loading = true
      // 验证表单内容是否合法，如果合法执行下一步
      this.$refs.loginRef.validate(valid => {
        if (!valid) return
        // 发送post请求，传递账号密码并返回令牌
        this.$http.post('manager', this.loginForm).then(res => {
          if (res.data.code !== 10000) {
            this.$message.error({
              message: '登录失败',
              duration: this.mtime
            })
            this.loading = false
          } else {
            this.$message.success({
              message: '登录成功',
              duration: this.mtime
            })
            this.loading = false
            window.sessionStorage.setItem('loginToken', res.data.data.token)
            window.sessionStorage.setItem('manger', res.data.data.name)
            this.$router.push('/main')
          }
        }).catch()
      })
    }
  }
}
</script>

<style scoped>
  #login-box{
    width: 100%;
    height: 100%;
    background: url("../assets/images/bgc.gif");
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .box-card{
    border-radius: 10px;
    background-color: white;
    width: 500px;
    height: 350px;
    box-shadow: 0 0 5px;
    position: relative;
  }
  .box-portrait{
    position: absolute;
    width: 150px;
    height: 150px;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    padding: 5px;
    background-color: #fff;
    border: 1px solid #fff;
  }
  .box-portrait img{
    width: 100%;
    height: 100%;
    border-radius: 50%;
  }
  .el-form{
    position: absolute;
    width: 100%;
    padding: 0 15px;
    bottom: 50px;
    box-sizing: border-box;
  }
  .btns{
    display: flex;
    justify-content: left;
  }
</style>
